package com.ipbd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ipbd.constant.QuestionnaireConstant;
import com.ipbd.domain.QuestionTime;
import com.ipbd.domain.QuestionTimeFive;
import com.ipbd.domain.Questionnaire;
import com.ipbd.dto.UserDTO;
import com.ipbd.dto.question.*;
import com.ipbd.mapper.QuestionTimeFiveMapper;
import com.ipbd.mapper.QuestionTimeMapper;
import com.ipbd.mapper.QuestionnaireMapper;
import com.ipbd.service.QuestionnaireService;
import com.ipbd.utils.UserHolder;
import com.ipbd.vo.QuestionnaireResult;
import com.ipbd.vo.Questionnaires;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 86172
 * @description 针对表【questionnaire(问卷调查表)】的数据库操作Service实现
 * @createDate 2024-03-06 20:53:48
 */
@Service
public class QuestionnaireServiceImpl extends ServiceImpl<QuestionnaireMapper, Questionnaire>
        implements QuestionnaireService {

    @Autowired
    private QuestionTimeMapper questionTimeMapper;
    @Autowired
    private QuestionTimeFiveMapper questionTimeFiveMapper;


    /**
     * 发送问卷信息
     *
     * @return
     */
    @Override
    public Questionnaires getQuestionnaire(int stage) {
        //TODO:添加阶段条件
        Questionnaires questionnaires = new Questionnaires();
        questionnaires.setQuestionnaireOne(getQuestionnaireOne(stage));
        questionnaires.setQuestionnaireTwo(getQuestionnaireTwo(stage));
        questionnaires.setQuestionnaireThree(getQuestionnaireThree(stage));
        questionnaires.setQuestionnaireFour(getQuestionnaireFour(stage));
        questionnaires.setQuestionnaireFive(getQuestionnaireFive(stage));
        questionnaires.setQuestionnaireSix(getQuestionnaireSix(stage));
        questionnaires.setStage(stage);
        return questionnaires;
    }

    public QuestionnaireOne getQuestionnaireOne(int stage) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        QueryWrapper<Questionnaire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id).eq("type", 1).eq("stage",stage);
        List<Questionnaire> questionnaireOneList = baseMapper.selectList(queryWrapper);
        List<Question> questionOneList = new ArrayList<>();
        for (Questionnaire questionnaire : questionnaireOneList) {
            Question questionOne = new Question();
            questionOne.setQuestionOption(questionnaire.getQuestionOption());
            questionOne.setQuestionOrder(questionnaire.getQuestionOrder());
            questionOneList.add(questionOne);
        }
        QuestionnaireOne questionnaireOne = new QuestionnaireOne();
        String name = QuestionnaireConstant.name(1);
        questionnaireOne.setName(name);
        questionnaireOne.setQuestions(questionOneList);
        return questionnaireOne;
    }

    private QuestionnaireTwo getQuestionnaireTwo(int stage) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        QueryWrapper<Questionnaire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id).eq("type", 2).eq("stage",stage);
        List<Questionnaire> questionnaireTwoList = baseMapper.selectList(queryWrapper);
        List<Question> questionTwoList = new ArrayList<>();
        for (Questionnaire questionnaire : questionnaireTwoList) {
            Question questionTwo = new Question();
            questionTwo.setQuestionOption(questionnaire.getQuestionOption());
            questionTwo.setQuestionOrder(questionnaire.getQuestionOrder());
            questionTwoList.add(questionTwo);
        }
        QuestionnaireTwo questionnaireTwo = new QuestionnaireTwo();
        String name = QuestionnaireConstant.name(2);
        questionnaireTwo.setName(name);
        questionnaireTwo.setQuestions(questionTwoList);
        return questionnaireTwo;
    }

    /**
     * 特殊问卷
     *
     * @return
     */
    private QuestionnaireThree getQuestionnaireThree(int stage) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        // 查询QuestionTime表中与当前用户相关的数据
        QueryWrapper<QuestionTime> questionTimeWrapper = new QueryWrapper<>();
        questionTimeWrapper.eq("user_id", id).eq("stage",stage);
        QuestionTime questionTime = questionTimeMapper.selectOne(questionTimeWrapper);

        // 查询QuestionTimeFive表中与当前用户相关的数据
        QueryWrapper<QuestionTimeFive> questionTimeFiveWrapper = new QueryWrapper<>();
        questionTimeFiveWrapper.eq("user_id", id).eq("stage",stage);
        List<QuestionTimeFive> questionTimeFiveList = questionTimeFiveMapper.selectList(questionTimeFiveWrapper);

        // 构建QuestionnaireTime对象并整理数据
        List<QuestionnaireTime> questionnaireTimeList = new ArrayList<>();
        if (questionTime != null) {
            QuestionnaireTime questionnaireTime = new QuestionnaireTime();
            questionnaireTime.setGoTime(questionTime.getGoTime());
            questionnaireTime.setAsleepTime(questionTime.getAsleepTime());
            questionnaireTime.setWakeTime(questionTime.getWakeTime());
            questionnaireTime.setSleepTime(questionTime.getSleepTime());
            questionnaireTime.setSleepQuality(questionTime.getSleepQuality());
            questionnaireTime.setMedicationStatus(questionTime.getMedicationStatus());
            questionnaireTime.setLevelTired(questionTime.getLevelTired());
            questionnaireTime.setWorkExperience(questionTime.getWorkExperience());

            // 设置QuestionnaireTimeFive数据
            List<QuestionnaireTimeFive> questionnaireTimeFiveList = new ArrayList<>();
            for (QuestionTimeFive questionTimeFive : questionTimeFiveList) {
                QuestionnaireTimeFive questionnaireTimeFive = new QuestionnaireTimeFive();
                questionnaireTimeFive.setA(questionTimeFive.getA());
                questionnaireTimeFive.setB(questionTimeFive.getB());
                questionnaireTimeFive.setC(questionTimeFive.getC());
                questionnaireTimeFive.setD(questionTimeFive.getD());
                questionnaireTimeFive.setE(questionTimeFive.getE());
                questionnaireTimeFive.setF(questionTimeFive.getF());
                questionnaireTimeFive.setG(questionTimeFive.getG());
                questionnaireTimeFive.setH(questionTimeFive.getH());
                questionnaireTimeFive.setI(questionTimeFive.getI());
                questionnaireTimeFive.setJ(questionTimeFive.getJ());
                questionnaireTimeFive.setRemarks(questionTimeFive.getRemarks());
                questionnaireTimeFiveList.add(questionnaireTimeFive);
            }
            questionnaireTime.setQuestionsFive(questionnaireTimeFiveList);
            questionnaireTimeList.add(questionnaireTime);
        }

        // 构建QuestionnaireThree对象并设置数据
        QuestionnaireThree questionnaireThree = new QuestionnaireThree();
        String name = QuestionnaireConstant.name(3);
        questionnaireThree.setName(name);
        questionnaireThree.setQuestions(questionnaireTimeList);
        return questionnaireThree;
    }


    /**
     * 需分组的问卷
     *
     * @return
     */
    private QuestionnaireFour getQuestionnaireFour(int stage) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        QueryWrapper<Questionnaire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id).eq("type", 4).eq("stage",stage);
        List<Questionnaire> questionnaireFourList = baseMapper.selectList(queryWrapper);
        QuestionnaireFour questionnaireFour = new QuestionnaireFour();
        String name = QuestionnaireConstant.name(4);
        questionnaireFour.setName(name);

        //设置范围
        List<Question> physiologicalStatusList = new ArrayList<>();
        List<Question> socialConditionList = new ArrayList<>();
        List<Question> emotionalStatusList = new ArrayList<>();

        for (Questionnaire questionnaire : questionnaireFourList) {
            Question question = new Question();
            question.setQuestionOrder(questionnaire.getQuestionOrder());
            question.setQuestionOption(questionnaire.getQuestionOption());

            int questionOrder = questionnaire.getQuestionOrder();
            if (questionOrder >= 1 && questionOrder <= 7) {
                // questionOrder1-7为PhysiologicalStatus
                physiologicalStatusList.add(question);
            } else if (questionOrder >= 8 && questionOrder <= 15) {
                // questionOrder8-15为SocialCondition
                socialConditionList.add(question);
            } else if (questionOrder >= 16 && questionOrder <= 21) {
                // questionOrder16-21为EmotionalStatus
                emotionalStatusList.add(question);
            }
        }

        questionnaireFour.setPhysiologicalStatus(physiologicalStatusList);
        questionnaireFour.setSocialCondition(socialConditionList);
        questionnaireFour.setEmotionalStatus(emotionalStatusList);

        return questionnaireFour;
    }

    private QuestionnaireFive getQuestionnaireFive(int stage) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        QueryWrapper<Questionnaire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id).eq("type", 5).eq("stage",stage);
        List<Questionnaire> questionnaireFiveList = baseMapper.selectList(queryWrapper);
        List<Question> questionFiveList = new ArrayList<>();
        for (Questionnaire questionnaire : questionnaireFiveList) {
            Question questionFive = new Question();
            questionFive.setQuestionOption(questionnaire.getQuestionOption());
            questionFive.setQuestionOrder(questionnaire.getQuestionOrder());
            questionFiveList.add(questionFive);
        }
        QuestionnaireFive questionnaireFive = new QuestionnaireFive();
        String name = QuestionnaireConstant.name(5);
        questionnaireFive.setName(name);
        questionnaireFive.setQuestions(questionFiveList);
        return questionnaireFive;
    }

    private QuestionnaireSix getQuestionnaireSix(int stage) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        QueryWrapper<Questionnaire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id).eq("type", 6).eq("stage",stage);
        List<Questionnaire> questionnaireSixList = baseMapper.selectList(queryWrapper);
        List<Question> questionSixList = new ArrayList<>();
        for (Questionnaire questionnaire : questionnaireSixList) {
            Question questionSix = new Question();
            questionSix.setQuestionOption(questionnaire.getQuestionOption());
            questionSix.setQuestionOrder(questionnaire.getQuestionOrder());
            questionSixList.add(questionSix);
        }
        QuestionnaireSix questionnaireSix = new QuestionnaireSix();
        String name = QuestionnaireConstant.name(6);
        questionnaireSix.setName(name);
        questionnaireSix.setQuestions(questionSixList);
        return questionnaireSix;
    }


    /**
     * 保存问卷信息
     *
     * @param questionnaires
     */
    @Override
    public void saveQuestionnaire(Questionnaires questionnaires) {
        saveQuestionnaireOne(questionnaires.getQuestionnaireOne());
        saveQuestionnaireTwo(questionnaires.getQuestionnaireTwo());
        saveQuestionnaireThree(questionnaires.getQuestionnaireThree());
        saveQuestionnaireFour(questionnaires.getQuestionnaireFour());
        saveQuestionnaireFive(questionnaires.getQuestionnaireFive());
        saveQuestionnaireSix(questionnaires.getQuestionnaireSix());
    }


    private void saveQuestionnaireOne(QuestionnaireOne questionnaireOne) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        List<Question> questions = questionnaireOne.getQuestions();
        for (Question question : questions) {
            Questionnaire questionnaire = new Questionnaire();
            questionnaire.setUserId(id);
            questionnaire.setType(1);
            questionnaire.setQuestionOrder(question.getQuestionOrder());
            questionnaire.setQuestionOption(question.getQuestionOption());
            baseMapper.insert(questionnaire);
        }
    }

    private void saveQuestionnaireTwo(QuestionnaireTwo questionnaireTwo) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        List<Question> questions = questionnaireTwo.getQuestions();
        for (Question question : questions) {
            Questionnaire questionnaire = new Questionnaire();
            questionnaire.setUserId(id);
            questionnaire.setType(2);
            questionnaire.setQuestionOrder(question.getQuestionOrder());
            questionnaire.setQuestionOption(question.getQuestionOption());
            baseMapper.insert(questionnaire);
        }
    }


    /**
     * 特殊问卷
     */
    private void saveQuestionnaireThree(QuestionnaireThree questionnaireThree) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        List<QuestionnaireTime> questionnaireTimeList = questionnaireThree.getQuestions();
        for (QuestionnaireTime questionnaireTime : questionnaireTimeList) {
            // 保存QuestionTime表中的信息
            QuestionTime questionTime = new QuestionTime();
            questionTime.setUserId(id);
            questionTime.setGoTime(questionnaireTime.getGoTime());
            questionTime.setAsleepTime(questionnaireTime.getAsleepTime());
            questionTime.setWakeTime(questionnaireTime.getWakeTime());
            questionTime.setSleepTime(questionnaireTime.getSleepTime());
            questionTime.setSleepQuality(questionnaireTime.getSleepQuality());
            questionTime.setMedicationStatus(questionnaireTime.getMedicationStatus());
            questionTime.setLevelTired(questionnaireTime.getLevelTired());
            questionTime.setWorkExperience(questionnaireTime.getWorkExperience());
            questionTimeMapper.insert(questionTime);

            // 保存QuestionTimeFive表中的信息
            List<QuestionnaireTimeFive> questionnaireTimeFiveList = questionnaireTime.getQuestionsFive();
            for (QuestionnaireTimeFive questionnaireTimeFive : questionnaireTimeFiveList) {
                QuestionTimeFive questionTimeFive = new QuestionTimeFive();
                questionTimeFive.setUserId(id);
                questionTimeFive.setA(questionnaireTimeFive.getA());
                questionTimeFive.setB(questionnaireTimeFive.getB());
                questionTimeFive.setC(questionnaireTimeFive.getC());
                questionTimeFive.setD(questionnaireTimeFive.getD());
                questionTimeFive.setE(questionnaireTimeFive.getE());
                questionTimeFive.setF(questionnaireTimeFive.getF());
                questionTimeFive.setG(questionnaireTimeFive.getG());
                questionTimeFive.setH(questionnaireTimeFive.getH());
                questionTimeFive.setI(questionnaireTimeFive.getI());
                questionTimeFive.setJ(questionnaireTimeFive.getJ());
                questionTimeFive.setRemarks(questionnaireTimeFive.getRemarks());
                questionTimeFiveMapper.insert(questionTimeFive);
            }
        }


    }

    private void saveQuestionnaireFour(QuestionnaireFour questionnaireFour) {
        List<Question> physiologicalStatusList = questionnaireFour.getPhysiologicalStatus();
        List<Question> socialConditionList = questionnaireFour.getSocialCondition();
        List<Question> emotionalStatusList = questionnaireFour.getEmotionalStatus();
        saveQuestions(physiologicalStatusList, 4);
        saveQuestions(socialConditionList, 4);
        saveQuestions(emotionalStatusList, 4);
    }

    private void saveQuestions(List<Question> questions, int type) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        for (Question question : questions) {
            Questionnaire questionnaire = new Questionnaire();
            questionnaire.setUserId(id);
            questionnaire.setType(type);
            questionnaire.setQuestionOrder(question.getQuestionOrder());
            questionnaire.setQuestionOption(question.getQuestionOption());
            baseMapper.insert(questionnaire);
        }
    }

    private void saveQuestionnaireFive(QuestionnaireFive questionnaireFive) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        List<Question> questions = questionnaireFive.getQuestions();
        for (Question question : questions) {
            Questionnaire questionnaire = new Questionnaire();
            questionnaire.setUserId(id);
            questionnaire.setType(5);
            questionnaire.setQuestionOrder(question.getQuestionOrder());
            questionnaire.setQuestionOption(question.getQuestionOption());
            baseMapper.insert(questionnaire);
        }
    }

    private void saveQuestionnaireSix(QuestionnaireSix questionnaireSix) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        // 将 questionnaireSix 中的信息保存到数据库中
        List<Question> questions = questionnaireSix.getQuestions();
        for (Question question : questions) {
            Questionnaire questionnaire = new Questionnaire();
            questionnaire.setUserId(id);
            questionnaire.setType(6);
            questionnaire.setQuestionOrder(question.getQuestionOrder());
            questionnaire.setQuestionOption(question.getQuestionOption());
            baseMapper.insert(questionnaire);
        }
    }

    @Override
    public QuestionnaireResult getQuestionnaireResult() {
        QuestionnaireResult questionnaireResult = new QuestionnaireResult();

        // 获取焦虑分数和级别
        int anxietyScore = getAnxietyScore();
        String anxietyLevel = getAnxietyLevel(anxietyScore);
        questionnaireResult.setAnxietyScore(anxietyScore);
        questionnaireResult.setAnxietyLevel(anxietyLevel);

        // 获取失眠分数和级别
        int sleeplessnessScore = getSleeplessnessScore();
        String sleeplessnessLevel = getSleeplessnessLevel(sleeplessnessScore);
        questionnaireResult.setSleeplessnessScore(sleeplessnessScore);
        questionnaireResult.setSleeplessnessLevel(sleeplessnessLevel);

        // 获取睡眠质量分数和级别
        int sleepScore = getSleepScore();
        String sleepLevel = getSleepLevel(sleepScore);
        questionnaireResult.setSleepScore(sleepScore);
        questionnaireResult.setSleepLevel(sleepLevel);

        // 获取生活质量分数和级别
        int lifeScore = getLifeScore();
        String lifeLevel = getLifeLevel(lifeScore);
        questionnaireResult.setLifeScore(lifeScore);
        questionnaireResult.setLifeLevel(lifeLevel);

        // 获取情绪自信分数和级别
        String emotionalConfidence = getEmotionalConfidence();
        String emotionalSelf = getEmotionalSelf();
        questionnaireResult.setEmotionalConfidence(emotionalConfidence);
        questionnaireResult.setEmotionalSelf(emotionalSelf);

        // 获取抑郁分数和级别
        int depressionScore = getDepressionScore();
        String depressionLevel = getDepressionLevel(depressionScore);
        questionnaireResult.setDepressionScore(depressionScore);
        questionnaireResult.setDepressionLevel(depressionLevel);

        return questionnaireResult;
    }


    private int getAnxietyScore() {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        // 在数据库中查询type为1的所有问题选项
        QueryWrapper<Questionnaire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id).eq("type", 1);
        List<Questionnaire> questionnaireList = baseMapper.selectList(queryWrapper);

        int totalScore = 0;
        int fullScore = 28; // 满分28

        // 遍历问题选项，计算总分
        for (Questionnaire questionnaire : questionnaireList) {
            totalScore += questionnaire.getQuestionOption();
        }

        // 如果总分大于满分，将总分设置为满分
        totalScore = Math.min(totalScore, fullScore);

        // 计算百分比
        int anxietyScore = (int) Math.round(((double) totalScore / fullScore) * 100);

        return anxietyScore;
    }


    private String getAnxietyLevel(int anxietyScore) {
        if (anxietyScore == 0) {
            return "无";
        } else if (anxietyScore >= 1 && anxietyScore <= 60) {
            return "轻度";
        } else if (anxietyScore > 60 && anxietyScore <= 85) {
            return "中度";
        } else if (anxietyScore > 85 && anxietyScore <= 100) {
            return "重度";
        } else {
            // 如果超出范围，默认返回“超出范围”
            return "超出范围";
        }
    }


    private int getSleeplessnessScore() {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        // 在数据库中查询type为2的所有问题选项
        QueryWrapper<Questionnaire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id).eq("type", 2);
        List<Questionnaire> questionnaireList = baseMapper.selectList(queryWrapper);

        int totalScore = 0;
        int fullScore = 32; // 满分32

        // 遍历问题选项，计算总分
        for (Questionnaire questionnaire : questionnaireList) {
            totalScore += questionnaire.getQuestionOption();
        }

        // 如果总分大于满分，将总分设置为满分
        totalScore = Math.min(totalScore, fullScore);

        // 计算百分比
        int sleeplessnessScore = (int) Math.round(((double) totalScore / fullScore) * 100);

        return sleeplessnessScore;
    }

    private String getSleeplessnessLevel(int sleeplessnessScore) {
        if (sleeplessnessScore == 0) {
            return "无失眠障碍";
        } else if (sleeplessnessScore > 0 && sleeplessnessScore <= 60) {
            return "怀疑失眠";
        } else if (sleeplessnessScore > 60 && sleeplessnessScore <= 100) {
            return "处于失眠状态";
        } else {
            // 如果超出范围，默认返回“超出范围”
            return "超出范围";
        }
    }

    private int getSleepScore() {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        // 在数据库中查询type为2的所有问题选项
        QueryWrapper<QuestionTime> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        QuestionTime questionTime = questionTimeMapper.selectOne(queryWrapper);

        int sleepTime = questionTime.getSleepTime();
        int sleepScore = 0;

        // 假设目标睡眠时间为 10
        int targetSleepTime = 10;

        // 计算睡眠时间得分，假设最高得分为 100
        if (sleepTime == targetSleepTime) {
            sleepScore = 100;
        } else {
            // 计算得分，离目标睡眠时间越远，得分越低
            int distance = Math.abs(sleepTime - targetSleepTime);
            sleepScore = Math.max(0, 100 - distance * 10); // 假设每相差一小时扣10分
        }

        return sleepScore;
    }


    private String getSleepLevel(int sleepScore) {
        if (sleepScore > 0 && sleepScore <= 60) {
            return "优秀";
        } else if (sleepScore > 0 && sleepScore <= 60) {
            return "良好";
        } else if (sleepScore > 60 && sleepScore <= 100) {
            return "较差";
        } else {
            // 如果超出范围，默认返回“超出范围”
            return "超出范围";
        }
    }

    private int getLifeScore() {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        // 在数据库中查询type为4的所有问题选项
        QueryWrapper<Questionnaire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id).eq("type", 4);
        List<Questionnaire> questionnaireList = baseMapper.selectList(queryWrapper);

        int totalScore = 0;
        int fullScore = 105; // 满分105

        // 遍历问题选项，计算总分
        for (Questionnaire questionnaire : questionnaireList) {
            totalScore += questionnaire.getQuestionOption();
        }

        // 如果总分大于满分，将总分设置为满分
        totalScore = Math.min(totalScore, fullScore);

        // 计算百分比
        double percentageScore = ((double) totalScore / fullScore) * 100.0;

        // 四舍五入取整数百分比
        return (int) Math.round(percentageScore);
    }


    private String getLifeLevel(int lifeScore) {
        if (lifeScore > 0 && lifeScore <= 60) {
            return "优";
        } else if (lifeScore > 0 && lifeScore <= 60) {
            return "良";
        } else if (lifeScore > 60 && lifeScore <= 100) {
            return "差";
        } else {
            // 如果超出范围，默认返回“超出范围”
            return "超出范围";
        }
    }

    private String getEmotionalConfidence() {
        int emotionScore = getEmotionalScore();
        if (emotionScore > 0 && emotionScore <= 60) {
            return "优";
        } else if (emotionScore > 0 && emotionScore <= 60) {
            return "良";
        } else if (emotionScore > 60 && emotionScore <= 100) {
            return "差";
        } else {
            // 如果超出范围，默认返回“超出范围”
            return "超出范围";
        }
    }

    private String getEmotionalSelf() {
        int emotionScore = getEmotionalScore();
        if (emotionScore > 0 && emotionScore <= 60) {
            return "优";
        } else if (emotionScore > 0 && emotionScore <= 60) {
            return "良";
        } else if (emotionScore > 60 && emotionScore <= 100) {
            return "差";
        } else {
            // 如果超出范围，默认返回“超出范围”
            return "超出范围";
        }
    }


    private int getEmotionalScore() {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        // 在数据库中查询type为5的所有问题选项
        QueryWrapper<Questionnaire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id).eq("type", 5);
        List<Questionnaire> questionnaireList = baseMapper.selectList(queryWrapper);

        int totalScore = 0;
        int fullScore = 85; // 满分85

        // 遍历问题选项，计算总分
        for (Questionnaire questionnaire : questionnaireList) {
            totalScore += questionnaire.getQuestionOption();
        }

        // 如果总分大于满分，将总分设置为满分
        totalScore = Math.min(totalScore, fullScore);

        // 计算百分比
        double percentageScore = ((double) totalScore / fullScore) * 100.0;

        // 四舍五入取整数百分比
        return (int) Math.round(percentageScore);
    }

    private int getDepressionScore() {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        // 在数据库中查询type为6的所有问题选项
        QueryWrapper<Questionnaire> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id).eq("type", 6);
        List<Questionnaire> questionnaireList = baseMapper.selectList(queryWrapper);

        int totalScore = 0;
        int fullScore = 80; // 满分80

        // 遍历问题选项，计算总分
        for (Questionnaire questionnaire : questionnaireList) {
            totalScore += questionnaire.getQuestionOption();
        }

        // 如果总分大于满分，将总分设置为满分
        totalScore = Math.min(totalScore, fullScore);

        // 计算百分比
        double percentageScore = ((double) totalScore / fullScore) * 100.0;

        // 四舍五入取整数百分比
        return (int) Math.round(percentageScore);
    }

    private String getDepressionLevel(int depressionScore) {
        if (depressionScore == 0) {
            return "无";
        } else if (depressionScore >= 1 && depressionScore <= 60) {
            return "轻度";
        } else if (depressionScore > 60 && depressionScore <= 85) {
            return "中度";
        } else if (depressionScore > 85 && depressionScore <= 100) {
            return "重度";
        } else {
            // 如果超出范围，默认返回“超出范围”
            return "超出范围";
        }
    }


}




